<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="交易单编号" prop="marketId" label-width="88px">
        <el-input v-model="queryParams.marketId" placeholder="请输入交易单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="接单人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入接单人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="接单时间" prop="receiveTime">
        <el-date-picker clearable v-model="queryParams.receiveTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择接单时间">
        </el-date-picker>
      </el-form-item>
      <!-- 转账状态 -->
      <el-form-item label="转账状态" prop="transferState">
        <el-select v-model="queryParams.transferState" placeholder="请选择转账状态" clearable @change="handleQuery">
          <el-option label="已转账" :value="1" />
          <el-option label="未转账" :value="0" />
        </el-select>
      </el-form-item>
      <!-- 支付状态 -->
      <el-form-item label="支付状态" prop="payState">
        <el-select v-model="queryParams.payState" placeholder="请选择支付状态" clearable @change="handleQuery">
          <el-option label="已支付" :value="1" />
          <el-option label="未支付" :value="0" />
        </el-select>
      </el-form-item>
      <!-- 支付方式 -->
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable @change="handleQuery">
          <el-option v-for="(item, index) in payWayList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['mall:mallMarketReceiveShangdou:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mall:mallMarketReceiveShangdou:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mall:mallMarketReceiveShangdou:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['mall:mallMarketReceiveShangdou:export']">导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallMarketReceiveShangdouList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="id" />
      <el-table-column label="交易单编号" align="center" prop="marketId" />
      <el-table-column label="接单人" align="center" prop="receiverUserTemp.userName" />
      <el-table-column label="接单时间" align="center" prop="receiveTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.receiveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务费" align="center" prop="servicePrice" v-if="route.query.sign == 1" />
      <el-table-column :label="`${route.query.sign == 1 ? '支付' : '购买'}熵豆数量`" align="center" prop="payShangdouNum" />
      <el-table-column label="转账信息" align="center" prop="transferTime" width="180">
        <template #default="scope">
          <p>转账金额:{{ scope.row.transferPrice }}</p>
          <p>转账状态:{{ scope.row.transferState ? '已转账' : '未转账' }}</p>
          <p>转账时间:{{ parseTime(scope.row?.transferTime) }}</p>
        </template>
      </el-table-column>
      <el-table-column label="支付信息" align="center" prop="transferTime" width="180" v-if="route.query.sign == 2">
        <template #default="scope">
          <p>支付金额:{{ scope.row.payPrice }}</p>
          <p>支付方式:{{ stateFun(scope.row.payWay, payWayList) }}</p>
          <p>支付状态:{{ scope.row.payState ? '已支付' : '未支付' }}</p>
          <p>支付时间:{{ parseTime(scope.row?.payTime) }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:mallMarketReceiveShangdou:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallMarketReceiveShangdou:remove']">删除</el-button> -->
          <el-button link type="primary" @click="handleFinish(scope.row)"
            v-if="route.query.sign == 2 && scope.row.payState == 1 && scope.row.transferState == 0">接单记录转账</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改熵豆交易单接取记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="mallMarketReceiveShangdouRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="交易单id" prop="marketId">
          <el-input v-model="form.marketId" placeholder="请输入交易单id" />
        </el-form-item>
        <el-form-item label="接单人" prop="receiver">
          <el-input v-model="form.receiver" placeholder="请输入接单人" />
        </el-form-item>
        <el-form-item label="接单时间" prop="receiveTime">
          <el-date-picker clearable v-model="form.receiveTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择接单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务费" prop="servicePrice">
          <el-input v-model="form.servicePrice" placeholder="请输入服务费" />
        </el-form-item>
        <el-form-item label="支付金豆数量" prop="payShangdouNum">
          <el-input v-model="form.payShangdouNum" placeholder="请输入支付金豆数量" />
        </el-form-item>
        <el-form-item label="转账金额" prop="transferPrice">
          <el-input v-model="form.transferPrice" placeholder="请输入转账金额" />
        </el-form-item>
        <el-form-item label="转账状态" prop="transferState">
          <el-input v-model="form.transferState" placeholder="请输入转账状态" />
        </el-form-item>
        <el-form-item label="转账时间" prop="transferTime">
          <el-date-picker clearable v-model="form.transferTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择转账时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MallMarketReceiveShangdou">
import { listMallMarketReceiveShangdou, getMallMarketReceiveShangdou, delMallMarketReceiveShangdou, addMallMarketReceiveShangdou, updateMallMarketReceiveShangdou, transfer } from "@/api/mall/mallMarketReceiveShangdou";

const { proxy } = getCurrentInstance();

const mallMarketReceiveShangdouList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();

const payWayList = ref([
  {
    label: "微信小程序",
    value: 0,
  },
  {
    label: "微信app",
    value: 1,
  },
  {
    label: "支付宝",
    value: 2,
  },
  {
    label: "支付宝",
    value: 3,
  },
  {
    label: "余额",
    value: 4,
  },
  {
    label: "熵豆",
    value: 10,
  },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    marketId: route.query?.id,
    receiver: null,
    receiveTime: null,
    servicePrice: null,
    payShangdouNum: null,
    transferPrice: null,
    transferState: null,
    transferTime: null,
    isDelete: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

const handleFinish = (row) => {
  proxy.$modal.confirm("确认接单记录转账吗？").then(function () {
    transfer({
      id: row.id,
      state: 1
    }).then((response) => {
      proxy.$modal.msgSuccess("操作成功");
      getList();
    });
  })
}

/** 查询熵豆交易单接取记录列表 */
function getList() {
  loading.value = true;
  listMallMarketReceiveShangdou(queryParams.value).then(response => {
    mallMarketReceiveShangdouList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    marketId: null,
    receiver: null,
    receiveTime: null,
    servicePrice: null,
    payShangdouNum: null,
    transferPrice: null,
    transferState: null,
    transferTime: null,
    isDelete: null
  };
  proxy.resetForm("mallMarketReceiveShangdouRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加熵豆交易单接取记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMallMarketReceiveShangdou(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改熵豆交易单接取记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mallMarketReceiveShangdouRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMallMarketReceiveShangdou(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMallMarketReceiveShangdou(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除熵豆交易单接取记录编号为"' + _ids + '"的数据项？').then(function () {
    return delMallMarketReceiveShangdou(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mall/mallMarketReceiveShangdou/export', {
    ...queryParams.value
  }, `mallMarketReceiveShangdou_${new Date().getTime()}.xlsx`)
}

getList();
</script>
